package com.usian.admin.controller.v1;

import com.usian.admin.entity.AdChannel;
import com.usian.admin.service.IAdChannelService;
import com.usian.common.dtos.PageResult;
import com.usian.common.dtos.ResponseResult;
import com.usian.common.enums.AppHttpCodeEnum;
import com.usian.common.exception.LeadException;
import com.usian.common.util.BeanHelper;
import com.usian.model.admin.dtos.ChannelDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 频道信息表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/api/v1/channel")
@Api(tags = "频道信息表接口")
@CrossOrigin
public class AdChannelController{

    @Autowired
    private IAdChannelService adChannelService;

    /**
     * 分页查询频道列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "根据名称分页查询频道列表")
    @ApiImplicitParam(name = "channelDto", value = "频道请求对象", required = true, dataType = "ChannelDto")
    public PageResult<ChannelDto> findByPage(@RequestBody ChannelDto channelDto){
       return adChannelService.findByPage(channelDto);
    }

    /**
     * 新增频道
     */
    @PostMapping("/save")
    public ResponseResult save(@RequestBody ChannelDto channelDto){
        //参数校验
        if(channelDto==null || StringUtils.isBlank(channelDto.getName())){
            return ResponseResult.error(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel adChannel = BeanHelper.copyProperties(channelDto, AdChannel.class);
        adChannel.setCreatedTime(new Date());
        boolean b = adChannelService.save(adChannel);
        if(!b){
            throw new LeadException(AppHttpCodeEnum.CHANNEL_SAVE_FAIL);
        }
        return ResponseResult.ok();
    }
    /**
     * 修改频道
     */
    @PutMapping("/update")
    public ResponseResult update(@RequestBody ChannelDto channelDto){
        //参数校验
        if(channelDto==null || StringUtils.isBlank(channelDto.getName())){
            return ResponseResult.error(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel adChannel = BeanHelper.copyProperties(channelDto, AdChannel.class);
        boolean b = adChannelService.updateById(adChannel);
        if(!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR,"修改频道失败");
        }
        return ResponseResult.ok();
    }

    /**
     * 删除频道 ，有效状态的频道不能删除
     */
    @DeleteMapping("/del/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id){
        adChannelService.deleteById(id);
        return ResponseResult.ok();
    }

    /**
     * 查询所有频道列表
     */
    @GetMapping("/channels")
    public ResponseResult<List<ChannelDto>> findAll(){
        List<AdChannel> channelList = adChannelService.list();
        if(CollectionUtils.isEmpty(channelList)){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST,"频道列表数据不存在");
        }
        List<ChannelDto> channelDtoList = BeanHelper.copyWithCollection(channelList, ChannelDto.class);
        return ResponseResult.ok(channelDtoList);
    }

}
